package com.song.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.commonutils.ResultObj;
import com.song.eduservice.entity.EduTeacher;
import com.song.eduservice.entity.vo.EduTeacherVo;
import com.song.eduservice.mapper.EduTeacherMapper;
import com.song.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author song
 * @since 2020-03-27
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    EduTeacherMapper eduTeacherMapper;
    @Override
    public ResultObj pageTeacherCondition(EduTeacherVo eduTeacherVo, long current, long limit) {
        Page<EduTeacher> page = new Page<>(current,limit);
        QueryWrapper<EduTeacher> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(eduTeacherVo.getName()),"name",eduTeacherVo.getName());
        qw.eq(eduTeacherVo.getLevel()!=null,"level",eduTeacherVo.getLevel());
        qw.ge(eduTeacherVo.getBegin()!=null,"gmt_create",eduTeacherVo.getBegin());
        qw.le(eduTeacherVo.getEnd()!=null,"gmt_create",eduTeacherVo.getEnd());
        qw.orderByDesc("gmt_create");
        eduTeacherMapper.selectPage(page,qw);
        return ResultObj.ok().data("total",page.getTotal()).data("data",page.getRecords());
    }

    @Override
    public Map<String, Object> getTeacherFront(Page<EduTeacher> page) {
        QueryWrapper<EduTeacher> qw=new QueryWrapper<>();
        qw.orderByDesc("id");
        baseMapper.selectPage(page,qw);
        List<EduTeacher> records = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();//下一页
        boolean hasPrevious = page.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

}
